﻿using System.IO;
using System.Web.Hosting;
using System.Web.Optimization;
using System.Web.UI;
using Newtonsoft.Json;

namespace Altairis.FluffyCloud.WebCore {

    internal static class BundleConfig {

        public static void RegisterBundles(BundleCollection bundles) {
            bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
                "~/Scripts/WebForms/WebForms.js",
                "~/Scripts/WebForms/WebUIValidation.js",
                "~/Scripts/WebForms/MenuStandards.js",
                "~/Scripts/WebForms/Focus.js",
                "~/Scripts/WebForms/GridView.js",
                "~/Scripts/WebForms/DetailsView.js",
                "~/Scripts/WebForms/TreeView.js",
                "~/Scripts/WebForms/WebParts.js"));
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                "~/Scripts/modernizr-*"));
            bundles.Add(new ScriptBundle("~/bundles/site").Include(
                "~/Scripts/jquery-dialogHelper.js",
                "~/Scripts/jquery-sewenEditor.js",
                "~/Scripts/jquery-textinputs.js",
                "~/Scripts/FluffyCloud/*.js"));

            ScriptManager.ScriptResourceMapping.AddDefinition("SiteJsBundle", new ScriptResourceDefinition { Path = "~/bundles/site" });

            RegisterThemes(bundles);
        }

        private static void RegisterThemes(BundleCollection bundles) {
            // Get JSON serialized list of themes
            var themesJson = File.ReadAllText(HostingEnvironment.MapPath("~/Content/Themes/list.json"));

            // Load it into structure
            var themes = new[] {new {
                Name = string.Empty,
                BundleUrl = string.Empty,
                Files = new string[] { string.Empty }
            }};
            themes = JsonConvert.DeserializeAnonymousType(themesJson, themes);

            // Register all theme bundles
            foreach (var theme in themes) {
                bundles.Add(new StyleBundle(theme.BundleUrl).Include(theme.Files));
            }
        }
    }
}